import com.mz.mattress.api.util.IdUtil;

public class CustomLockTask implements Runnable {

    private CustomLock customLock;

    public CustomLockTask(CustomLock customLock) {
        this.customLock = customLock;
    }

    public void doAction() {

        if (customLock.tryLock()) {
            try {
                System.out.println(Thread.currentThread().getName() + " 获取锁成功");
                Thread.sleep(1000); // 模拟资源占用
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                customLock.unlock();
                System.out.println(Thread.currentThread().getName() + " 释放锁");
            }
        } else {
            System.out.println(Thread.currentThread().getName() + " 获取锁失败");
        }
    }


    @Override
    public void run() {

        for (int i = 0; i < 1000; i++) {

            doAction();

        }

    }
}
